import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:gold/base/config.dart';
import 'package:gold/model/order_model.dart';
import 'package:gold/utils/res.dart';
import 'package:gold/utils/xfg_color.dart';
import 'package:gold/utils/xfg_size.dart';
import 'package:gold/widgets/common_widgets.dart';
import 'package:gold/widgets/http_image.dart';
import 'package:gold/widgets/xfg_text.dart';
import 'package:url_launcher/url_launcher.dart';

/// @description: 物流信息
/// @author tangzhentao
/// @created at 20:53 2024/12/13

class LogisticsPage extends StatelessWidget {
  /// 商品信息
  final OrderDetailProduct product;
  /// 收货地址
  final OrderAddress address;
  /// 物流信息
  final ExpressDetail express;
  
  const LogisticsPage({super.key, required this.product, required this.address,required this.express});

  @override
  Widget build(BuildContext context) {
    final expressStatus = getExpressStatusDesc(express.status);
    final mailNo = express.mailNo?? '';
    final phone = express.tel?? '';
    final list = express.data?? [];
    return Scaffold(
        appBar: xfgAppbar(title: '物流详情'),
        body: Padding(
          padding: EdgeInsets.symmetric(horizontal: XfgSize().size10),
          child: Column(
            children: [
              /// 商品信息，复制，电话
              Container(
                margin: EdgeInsets.symmetric(vertical: XfgSize().size10),
                decoration: white6Decoration,
                padding: EdgeInsets.all(XfgSize().size10),
                child: Column(
                  children: [
                    SizedBox(
                      height: XfgSize().size50,
                      child: Row(
                        children: [
                          HttpImage(product.coverImage, size: Size.square(XfgSize().size50), radius: BorderRadius.circular(XfgSize().size2)),
                          SizedBox(width: XfgSize().size10),
                          Expanded(
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                XfgText(expressStatus, fontSize: XfgSize().text18, color: XfgColor.text1, bold: FontWeight.bold),
                                XfgText(product.name?? '', fontSize: XfgSize().text14, color: XfgColor.text3, bold: FontWeight.bold, maxLines: 1, overflow: TextOverflow.ellipsis),
                              ],
                            ),
                          )
                        ],
                      ),
                    ),
                    Container(
                      height: XfgSize().size56,
                      padding: EdgeInsets.only(bottom: XfgSize().size15),
                      alignment: Alignment.bottomCenter,
                      child: Row(
                        children: [
                          Expanded(child: XfgText('${express.expTextName?? ''}  $mailNo', fontSize: XfgSize().text14, color: XfgColor.text3)),
                          Builder(builder: (con) => Visibility(
                            visible: mailNo.isNotEmpty,
                            child: GestureDetector(
                              onTap: () {
                                if (mailNo.isNotEmpty) {
                                  Clipboard.setData(ClipboardData(text: mailNo)).then((_) => xfgShowSnackBar(con, '快递单号', mailNo));
                                }
                              },
                              child: XfgText('复制', fontSize: XfgSize().text14, color: XfgColor.text1D),
                            ),
                          )),
                          Visibility(
                            visible: mailNo.isNotEmpty && phone.isNotEmpty,
                            child: Container(
                                margin: EdgeInsets.only(left: XfgSize().size14, right: XfgSize().size14),
                                width: XfgSize().size0_5, height: XfgSize().size10, color: XfgColor.text3
                            ),
                          ),
                          Visibility(
                            visible: phone.isNotEmpty,
                            child: GestureDetector(
                              onTap: () async {
                                final uri = Uri.parse('tel:$phone');
                                if (await canLaunchUrl(uri)) {
                                await launchUrl(uri);
                                }
                              },
                              child: XfgText('电话', fontSize: XfgSize().text14, color: XfgColor.text1D),
                            ),
                          )
                        ],
                      ),
                    )
                  ],
                ),
              ),
              /// 物流详情
              Expanded(
                child: Container(
                  decoration: white6Decoration,
                  padding: EdgeInsets.only(top: XfgSize().size25),
                  child: SingleChildScrollView(
                    padding: EdgeInsets.only(right: XfgSize().size10, bottom: XfgSize().bottomBarHeight == 0 ? XfgSize().size40:XfgSize().bottomBarHeight),
                    child: Column(
                      children: [
                        /// 收
                        IntrinsicHeight(
                          child: Row(
                            children: [
                              SizedBox(width: XfgSize().size84),
                              SizedBox(
                                width: XfgSize().size25 + XfgSize().size0_5,
                                child: Stack(
                                  children: [
                                    Container(
                                      margin: EdgeInsets.symmetric(horizontal: XfgSize().size12 + XfgSize().size0_5),
                                      width: XfgSize().size0_5,
                                      color: XfgColor.text3,
                                    ),
                                    Image.asset(Res.imagesLogisticsAccept, width: XfgSize().size25, height: XfgSize().size25)
                                  ],
                                ),
                              ),
                              SizedBox(width: XfgSize().size10),
                              Expanded(
                                child: Column(
                                  children: [
                                    XfgText('[收件地址]${address.address?? ''} ${address.houseNumber?? ''}  ${address.name} ${address.phone}', fontSize: XfgSize().text12, color: XfgColor.text3),
                                    SizedBox(height: list.isNotEmpty ? XfgSize().size32:0)
                                  ],
                                ),
                              )
                            ],
                          ),
                        ),
                        ...list.map((data) {
                          final index = list.indexOf(data);
                          final isLast = index == list.length - 1;
                          final isFirst = index == 0;
                          return Column(
                            children: [
                              IntrinsicHeight(
                                child: Row(
                                  children: [
                                    Container(
                                      width: XfgSize().size84,
                                      padding: EdgeInsets.only(right: XfgSize().size8),
                                      child: XfgText(data.time?.replaceAll(' ', '\n')?? '', fontSize: XfgSize().text12, color: isFirst ? XfgColor.text1:XfgColor.text3, textAlign: TextAlign.end, height: 1.5),
                                    ),
                                    SizedBox(
                                      width: XfgSize().size25 + XfgSize().size0_5,
                                      child: Stack(
                                        alignment: Alignment.center,
                                        children: [
                                          SizedBox(
                                            width: XfgSize().size0_5,
                                            child: Column(
                                              children: [
                                                Expanded(child: Container(width: XfgSize().size0_5, color: XfgColor.text3)),
                                                isLast ? const Spacer():Expanded(child: Container(width: XfgSize().size0_5, color: XfgColor.text3))
                                              ],
                                            ),
                                          ),
                                          isFirst ?
                                          Image.asset(Res.imagesLogisticsWay, width: XfgSize().size25, height: XfgSize().size25)
                                              :
                                          Container(width: XfgSize().size6, height: XfgSize().size6, decoration: const BoxDecoration(shape: BoxShape.circle, color: XfgColor.text3)),
                                        ],
                                      ),
                                    ),
                                    SizedBox(width: XfgSize().size10),
                                    Expanded(child: isFirst ? XfgText(expressStatus, fontSize: XfgSize().text18, color: XfgColor.theme, bold: FontWeight.bold):XfgText(data.context?? '', fontSize: XfgSize().text12, color: XfgColor.text3, height: 1.5))
                                  ],
                                ),
                              ),
                              Visibility(
                                visible: isFirst,
                                child: IntrinsicHeight(
                                  child: Row(
                                    children: [
                                      SizedBox(width: XfgSize().size84),
                                      Container(
                                        margin: EdgeInsets.only(left: XfgSize().size12 + XfgSize().size0_5, right: XfgSize().size22 + XfgSize().size0_5),
                                        width: XfgSize().size0_5,
                                        color: XfgColor.text3,
                                      ),
                                      Expanded(
                                        child: Container(
                                          margin: EdgeInsets.only(top: XfgSize().size6),
                                          decoration: BoxDecoration(color: XfgColor.background, borderRadius: BorderRadius.circular(XfgSize().size6)),
                                          padding: EdgeInsets.symmetric(horizontal: XfgSize().size16, vertical: XfgSize().size12),
                                          child: XfgText(data.context?? '', fontSize: XfgSize().text12, color: XfgColor.text1, height: 1.5),
                                        ),
                                      )
                                    ],
                                  ),
                                ),
                              ),
                              Visibility(visible: !isLast, child: _lineWidget(XfgSize().size32))
                            ],
                          );
                        })
                      ],
                    ),
                  ),
                ),
              )
            ],
          ),
        ),
    );
  }

  /// 连接线
  Widget _lineWidget(double height) => Row(
    children: [
      SizedBox(width: XfgSize().size84),
      Container(
        width: XfgSize().size25 + XfgSize().size0_5,
        height: height,
        alignment: Alignment.center,
        child: Container(
          width: XfgSize().size0_5,
          height: height,
          color: XfgColor.text3,
        )
      ),
      const Spacer(),
    ],
  );
} 
